package com.citycloud.ccuap.tc.admin.datacuser;

import com.citycloud.ccuap.commons.json.JSONData;
import com.citycloud.ccuap.commons.openapi.controller.base.BaseController;
import com.citycloud.ccuap.commons.sys.entity.SysDeptment;
import com.citycloud.ccuap.commons.sys.entity.SysGroup;
import com.citycloud.ccuap.commons.sys.entity.SysProjectDeptmentRel;
import com.citycloud.ccuap.commons.sys.group.IGroupService;
import com.citycloud.ccuap.commons.sys.group.SysGroupDTO;
import com.citycloud.ccuap.commons.sys.repository.SysDeptmentRepository;
import com.citycloud.ccuap.commons.sys.repository.SysGroupRepository;
import com.citycloud.ccuap.commons.sys.repository.SysProjectDeptmentRelRepository;
import com.citycloud.ccuap.commons.web.util.ResolveRequest;
import com.citycloud.ccuap.framework.exception.AppAssert;
import com.citycloud.ccuap.framework.mybatis.pagination.PageQuery;
import com.citycloud.ccuap.tc.admin.repository.SysProjectFunctionRepository;
import com.citycloud.ccuap.tc.admin.sec.secuser.SecUserMapper;
import com.google.common.collect.Lists;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author liujw
 * @create 2020-02-10 15:03 Description:
 */

@Slf4j
@RestController
@Api(tags = "Datac 同步下游数据控制类", value = "Datac 同步下游数据控制类-DatacUserController")
@RequestMapping(value = "/v1/sys/datac/datacUser")
public class DatacUserController extends BaseController {

	@Resource
	private SysDeptmentRepository deptmentRepository;
	@Resource
	private SysProjectDeptmentRelRepository projectDeptmentRelRepository;
	@Resource
	private SysProjectFunctionRepository sysProjectFunctionRepository;
	@Resource
	@Lazy
	private SecUserMapper secUserMapper;

	@Autowired
	private SysGroupRepository sysGroupRepository;

	@Autowired
	private IGroupService groupService;

	@ApiOperation("获取某一个部门下的所有部门,不包含子部门 若不传部门PID则获取根节点数据")
	@RequestMapping(value = "/getDirectDeptmentsByDeptId", method = { RequestMethod.POST,
			RequestMethod.GET }, produces = "application/json; charset=UTF-8")
	public JSONData getDirectDeptmentsByDeptId(@RequestParam(value = "deptId") Long deptId,
			@RequestParam(value = "projectId") Long projectId) {

		List<SysDeptment> deptments;
		if (null == deptId) {
			deptments = deptmentRepository.findAllByPidIsNull();
		} else {
			deptments = deptmentRepository.findByPid(deptId);
		}

		// 获取当前站点所有部门id(包含已导入的部门和自建部门)
		List<SysProjectDeptmentRel> projectDeptList = projectDeptmentRelRepository.getByProjectId(projectId);
		List<Long> deptIds = Lists.newArrayList();
		projectDeptList.stream().forEach(s -> {
			Long dId = s.getDeptId();
			deptIds.add(dId);
		});
		// 站点管理员自建部门
		Optional.ofNullable(deptmentRepository.findByProjectId(projectId)).orElseGet(() -> {
			return Lists.newArrayList();
		}).stream().forEach(s -> {
			Long dId = s.getDeptId();
			deptIds.add(dId);
		});

		// 过滤全量部门
		List<SysDeptment> reList = Lists.newArrayList();
		deptments.stream().forEach(s -> {
			Long temp = s.getDeptId();
			if (deptIds.contains(temp)) {
				reList.add(s);
			}
		});
		return new JSONData(reList);
	}

	@ApiOperation("获取某一个部门下的人员仅限于当前部门不包含子部门的人员")
	@RequestMapping(value = "/getDirectUsersByDeptId", method = { RequestMethod.POST,
			RequestMethod.GET }, produces = "application/json; charset=UTF-8")
	public JSONData getDirectUsersByDeptId(@RequestParam(value = "deptId") Long deptId,
			@RequestParam(value = "projectId") Long projectId) {

		if (null == deptId) {
			return new JSONData(false, "请输入部门ID参数！");
		}
		List<Map<String, Object>> temp = secUserMapper.getDatacListUsersByDeptId(deptId, projectId);

		// 自建人员
		List<Map<String, Object>> users = secUserMapper.getDatacListUsersByDeptIdAndProjectId(deptId, projectId);
		if (CollectionUtils.isNotEmpty(users)) {
			temp.addAll(users);
		}
		return new JSONData(temp);
	}

	@ApiOperation("根据部门编号获取部门信息")
	@RequestMapping(value = "/findGroupByNo",
			method = {RequestMethod.GET, RequestMethod.POST},
			produces = "application/json; charset=UTF-8")
	public JSONData findGroupByNo(@RequestParam(value = "groupNo") String groupNo) {
		AppAssert.isTrue(StringUtils.isNotEmpty(groupNo), "请传参部门编号！");

		SysGroup group;
		List<SysGroup> groupList = sysGroupRepository.findByGroupNo(groupNo);
		if (groupList.isEmpty()) {
			group = new SysGroup();
		} else {
			group = groupList.get(0);
		}
		JSONData jsonData = new JSONData(group);
		return jsonData;
	}

	@ApiOperation("查找机构树节点")
	@RequestMapping(value = "/findNextChildGroupNodeTree",
			method = {RequestMethod.GET, RequestMethod.POST},
			produces = "application/json; charset=UTF-8")
	public JSONData findNextChildGroupNodeTree(@RequestParam(value = "groupId") Long groupId) {
		List<SysGroup> nodeList;
		if (groupId == null) {
			nodeList = sysGroupRepository.findByPidNullOrderByOrderNo();
		} else {
			nodeList = sysGroupRepository.findByPidOrderByOrderNo(groupId);
		}
		JSONData jsonData = new JSONData(nodeList);
		return jsonData;
	}

	@ApiOperation("根据条件查询对象")
	@RequestMapping(value = "/queryGroup", method = RequestMethod.GET)
	public JSONData query() throws Exception {
		PageQuery<SysGroup> pageQuery = ResolveRequest.getPageForParams(request);
		SysGroupDTO sysGroupDTO = new SysGroupDTO();
		ResolveRequest.fillData(request, sysGroupDTO, "q_", true);

		PageQuery<SysGroup> pageData = groupService.selectPage(pageQuery, sysGroupDTO);

		JSONData jsonData = new JSONData(pageData);
		return jsonData;
	}

}
